import { Component, Host, ChangeDetectionStrategy, ChangeDetectorRef, ViewChild } from "@angular/core";
import { AppComponent } from "../../../../../../app.component";
import { ActivatedRoute } from "@angular/router";
import { Subscription } from "rxjs";
import { CompanyGovernanceService } from "../../companyGovernance.service";
import { CompanyService } from "../../../company.service";
import { CompanyGovernanceIssueDetailModel } from "../../models/companyGovernanceIssueDetail.model";
import { UtilsService } from "../../../../../../shared/services/utils.service";
import { AssessmentList } from "../../../../../../shared/models/assessment.enum";
import { GovernanceIndicatorModel } from "../../models/companyGovernanceIndicator.model";
import { BaseComponent } from "../../../../../../shared/components/baseComponent/base.component";
import { AnalyticsService } from "../../../../../../shared/services/googleAnalytics.service";
import { GoogleAnalyticsBeacon } from "../../../../../../shared/models/googleAnalytics.model";
import { GovernanceProfileModel } from "../../models/governanceProfile.model";

/**
 * Company governance issue component
 *
 * Author: Sorin Spiru
 * Date: 26.02.2018
 */
@Component({
    moduleId: module.id,
    templateUrl: 'companyGovernanceIssue.component.html',
    styleUrls: [
        '../../../company.component.scss',
        '../companyGovernanceSummary/companyGovernanceSummary.component.scss'
    ],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class CompanyGovernanceIssueComponent extends BaseComponent {
    private sub: Subscription;
    public profileSub: Subscription;
    public issue: CompanyGovernanceIssueDetailModel;
    public indicators: GovernanceIndicatorModel[];
    public governanceProfile: GovernanceProfileModel;
    public assessmentList: typeof AssessmentList = AssessmentList;
    public companyId: string;
    constructor(
        @Host() private rootComponent: AppComponent,
        private cdRef: ChangeDetectorRef,
        private companyService: CompanyService,
        private companyGovernanceService: CompanyGovernanceService,
        private route: ActivatedRoute,
        private analyticsService: AnalyticsService) {
        super(cdRef);
    }

    ngOnInit() {
        this.sub = this.route.params.subscribe(params => {
            let number: string = params['number'];
            window.scroll(0, 0);
            
            this.profileSub = this.companyGovernanceService.governanceProfile.subscribe(data => {
                this.governanceProfile = data;
                this.cdRef.markForCheck();
            }, error => this.errorCallback(_ => { this.governanceProfile = undefined; }, error));

            this.companyService.company.subscribe(company => {    
                this.companyId = company.id;   
                this.companyGovernanceService.getIssueDetails(company.id, number).subscribe(data => {                    
                    this.issue = data;
                    this.analyticsService.sendBeacon(new GoogleAnalyticsBeacon('CG Pillars', 'Research', { CompanyId: company.id, PillarName: this.issue.issue.name }));
                    this.cdRef.markForCheck();
                    this.rootComponent.finishLoading();
                }, error => this.errorCallback( _ => { this.issue = undefined }, error));

                this.companyGovernanceService.getIssueIndicators(company.id, number).subscribe(data => {
                    this.indicators = data;
                    this.cdRef.markForCheck();
                }, error => this.errorCallback( _ => { this.indicators = undefined }, error))
            });
        });
    }

    ngOnDestroy()
    {
        this.profileSub.unsubscribe();
    }

    public getPercentileOrdinal(): string {
        return UtilsService.getOrdinalNameEnd(this.issue.percentile);
    }

    public getIssueIcon( issueName: string ): string {
        return UtilsService.getIconFilename(issueName, 'governance');
    }

    public getIssueOutlook( issue ): string {
        switch (issue.outlook) {
            case -1:
                return 'Negative';
            case 0:
                return 'Neutral';
            case 1:
                return 'Positive';
        }
    }
}